//声明栈
typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

void STInit(ST* pst);		//初始化
void STDestroy(ST* pst);	//销毁
void STPush(ST* pst, STDataType x);	//入栈
void STPop(ST* pst);	//出栈
STDataType STTop(ST* pst);	//取栈顶元素
bool STEmpty(ST* pst);	//判空
int STSize(ST* pst);	//获取数据个数

//top指向栈中数据的下一个位置
void STInit(ST* pst)
{
	assert(pst);
	pst->a = NULL;
	pst->top = 0;
	pst->capacity = 0;
}

void STDestroy(ST* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->top = 0;
	pst->capacity = 0;
}

void STPush(ST* pst, STDataType x)
{
	assert(pst);
	if (pst->top == pst->capacity)
	{
		int newcapacity = pst->capacity == 0 ? 4 : (pst->capacity * 2);
		STDataType* temp = (STDataType*)realloc(pst->a, newcapacity * sizeof(ST));
		if (temp == NULL)
		{
			perror("realloc fail!\n");
			return;
		}
		pst->a = temp;
		pst->capacity = newcapacity;
	}
	pst->a[pst->top] = x;
	pst->top++;
}

void STPop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	pst->top--;
}

STDataType STTop(ST* pst)
{
	assert(pst);
	assert(pst->top > 0);
	return pst->a[pst->top - 1];
}

bool STEmpty(ST* pst)
{
	assert(pst);
	return pst->top == 0;
}

int STSize(ST* pst)
{
	assert(pst);
	return pst->top;
}

typedef struct 
{
    ST PushST;
    ST PopST;   
}MyQueue;


MyQueue* myQueueCreate()
{
    MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
    STInit(&(obj->PushST));
    STInit(&(obj->PopST));
    return obj;    
}

void myQueuePush(MyQueue* obj, int x)
{
    STPush(&(obj->PushST), x);
}

int myQueuePop(MyQueue* obj)
{
    if(STEmpty(&(obj->PopST)))
    {
        while(!STEmpty(&(obj->PushST)))
        {
            STPush(&(obj->PopST), STTop(&(obj->PushST)));
            STPop(&(obj->PushST));
        }
    }
    int data = STTop(&(obj->PopST));
    STPop(&(obj->PopST));
    return data;
}

int myQueuePeek(MyQueue* obj)
{
    if(STEmpty(&(obj->PopST)))
    {
        return obj->PushST.a[0];
    }
    return STTop(&(obj->PopST));
}

bool myQueueEmpty(MyQueue* obj) 
{
    return STEmpty(&(obj->PushST)) && STEmpty(&(obj->PopST));
}

void myQueueFree(MyQueue* obj)
{
    STDestroy(&(obj->PushST));
    STDestroy(&(obj->PopST));
    free(obj);
}